﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;

namespace NetfractionHub.LocalMachine.BotCommands.Handler
{
    [CommandHandler("setpermission", TriggerAlias="setperm")]
    public class SetPermission : CommandHandler
    {
        public override BotReturnStatus HandleCommandMessage(NetfractionHub.RemoteMachine.Node source, CommandMessage message)
        {
            // TODO: fix this method!
            /*if (source != null)
            {
                Security.AccessPermissionBase argPermission;

                if (message.StringArguments.ContainsKey("user"))
                {
                    if (message.StringArguments.ContainsKey("grant"))
                    {
                        if (!Security.PermissionBuilder.TryParse(message.StringArguments["grant"], out argPermission))
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, String.Format("The specified permission '{0}' does not exist.", message.StringArguments["grant"]));
                            return false;
                        }

                        if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.GrantPermissionPermission)))
                        {
                            Console.WriteLine("Granting '{0}' the permission: {1}", message.StringArguments["user"], argPermission);
                            RemoteMachine.Node granteeIdentity = Network.ConnectionManager.GetConnectedNodeByNickname(message.StringArguments["user"]);
                            if (granteeIdentity != null)
                            {
                                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, granteeIdentity.SecurityIdentity.Role))
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission denied. You are not an ancestor of user '{0}'.", message.StringArguments["user"]));
                                    return false;
                                }

                                if (!Security.Permission.HasPermission(granteeIdentity.SecurityIdentity, argPermission))
                                {
                                    if (granteeIdentity.SecurityIdentity.Permissions.ContainsKey(argPermission))
                                    {
                                        granteeIdentity.SecurityIdentity.Permissions[argPermission] = NetfractionHub.Security.AccessState.Allow;
                                    }
                                    else
                                    {
                                        granteeIdentity.SecurityIdentity.Permissions.Add(argPermission, Security.AccessState.Allow);
                                    }
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' granted to user '{1}'.", message.StringArguments["grant"], message.StringArguments["user"]));
                                }
                                else
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' already held by user '{1}'.", message.StringArguments["grant"], message.StringArguments["user"]));
                                }
                            }
                        }
                        else
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, Security.Permission.GetDeniedString(typeof(Security.Permissions.GrantPermissionPermission)));
                        }
                    }
                    else if (message.StringArguments.ContainsKey("deny"))
                    {
                        if (!Security.PermissionBuilder.TryParse(message.StringArguments["deny"], out argPermission))
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, String.Format("The specified permission '{0}' does not exist.", message.StringArguments["deny"]));
                            return false;
                        }

                        if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.DenyPermissionPermission)))
                        {
                            Console.WriteLine("Denying '{0}' the permission: {1}", message.StringArguments["user"], argPermission);
                            RemoteMachine.Node deniedIdentity = Network.ConnectionManager.GetConnectedNodeByNickname(message.StringArguments["user"]);
                            if (deniedIdentity != null)
                            {
                                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, deniedIdentity.SecurityIdentity.Role))
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission denied. You are not an ancestor of user '{0}'.", message.StringArguments["user"]));
                                    return false;
                                }

                                if (Security.Permission.HasPermission(deniedIdentity.SecurityIdentity, argPermission))
                                {
                                    if (deniedIdentity.SecurityIdentity.Permissions.ContainsKey(argPermission))
                                    {
                                        deniedIdentity.SecurityIdentity.Permissions[argPermission] = NetfractionHub.Security.AccessState.Deny;
                                    }
                                    else
                                    {
                                        deniedIdentity.SecurityIdentity.Permissions.Add(argPermission, Security.AccessState.Deny);
                                    }
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' denied for user '{1}'.", message.StringArguments["deny"], message.StringArguments["user"]));
                                }
                                else
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' already denied for user '{1}'.", message.StringArguments["deny"], message.StringArguments["user"]));
                                }
                            }
                        }
                        else
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, Security.Permission.GetDeniedString(typeof(Security.Permissions.DenyPermissionPermission)));
                        }
                    }
                }
                else if (message.StringArguments.ContainsKey("role"))
                {
                    if (message.StringArguments.ContainsKey("grant"))
                    {
                        if (!Security.PermissionBuilder.TryParse(message.StringArguments["grant"], out argPermission))
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, String.Format("The specified permission '{0}' does not exist.", message.StringArguments["grant"]));
                            return false;
                        }

                        if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.GrantPermissionPermission)))
                        {
                            Console.WriteLine("Granting role '{0}' the permission: {1}", message.StringArguments["role"], argPermission);
                            Security.Role granteeRole = Security.RoleManager.GetRoleByName(message.StringArguments["role"]);
                            if (granteeRole != null)
                            {
                                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, granteeRole))
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission denied. Your role is not an ancestor of role '{0}'.", message.StringArguments["role"]));
                                    return false;
                                }

                                if (!Security.Permission.HasPermission(granteeRole, argPermission))
                                {
                                    if (granteeRole.Permissions.ContainsKey(argPermission))
                                    {
                                        granteeRole.Permissions[argPermission] = NetfractionHub.Security.AccessState.Allow;
                                    }
                                    else
                                    {
                                        granteeRole.Permissions.Add(argPermission, Security.AccessState.Allow);
                                    }
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' granted to role '{1}'.", argPermission, message.StringArguments["role"]));
                                }
                                else
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' already held by role '{1}'.", argPermission, message.StringArguments["role"]));
                                }
                            }
                        }
                        else
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, Security.Permission.GetDeniedString(typeof(Security.Permissions.GrantPermissionPermission)));
                        }
                    }
                    else if (message.StringArguments.ContainsKey("deny"))
                    {
                        if (!Security.PermissionBuilder.TryParse(message.StringArguments["deny"], out argPermission))
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, String.Format("The specified permission '{0}' does not exist.", message.StringArguments["grant"]));
                            return false;
                        }

                        if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.DenyPermissionPermission)))
                        {
                            Console.WriteLine("Denying role '{0}' the permission: {1}", message.StringArguments["role"], argPermission);
                            Security.Role deniedRole = Security.RoleManager.GetRoleByName(message.StringArguments["role"]);
                            if (deniedRole != null)
                            {
                                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, deniedRole))
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission denied. Your role is not an ancestor of role '{0}'.", message.StringArguments["role"]));
                                    return false;
                                }

                                if (Security.Permission.HasPermission(deniedRole, argPermission))
                                {
                                    if (deniedRole.Permissions.ContainsKey(argPermission))
                                    {
                                        deniedRole.Permissions[argPermission] = NetfractionHub.Security.AccessState.Deny;
                                    }
                                    else
                                    {
                                        deniedRole.Permissions.Add(argPermission, Security.AccessState.Deny);
                                    }
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' denied for role '{1}'.", argPermission, message.StringArguments["role"]));
                                }
                                else
                                {
                                    this.ParentBot.SendEchoMessageFromBot(source, String.Format("Permission '{0}' already denied for role '{1}'.", argPermission, message.StringArguments["role"]));
                                }
                            }
                        }
                        else
                        {
                            this.ParentBot.SendEchoMessageFromBot(source, Security.Permission.GetDeniedString(typeof(Security.Permissions.DenyPermissionPermission)));
                        }
                    }
                }
                else
                {
                    Console.WriteLine("no user or role specified");
                }
            }*/
            return new BotReturnStatus(true);
        }
    }
}
